package com.car.carsharing.controller;

import com.car.carsharing.dto.ResponseDTO;
import com.car.carsharing.pojo.UserOrder;
import com.car.carsharing.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
public class OrderController {

    @Autowired
    private OrderService orderService;

    @RequestMapping("order")
    public ResponseDTO order(@RequestBody Map map){
        Integer userId   = (Integer) map.get("userId");
        Integer carId   = (Integer) map.get("carId");
        int r = orderService.addOrder(userId,carId);
        if(r == -1){
            return new ResponseDTO().error("预定失败");
        }
        return  new ResponseDTO().success("预定成功");
    }

    @RequestMapping("getMyOrder")
    public ResponseDTO getMyOrder(Integer userId){
        List<UserOrder> userOrder = orderService.getUserOrder(userId);
        return  new ResponseDTO().success(userOrder,"获取成功");
    }

    @RequestMapping("cancelOrder")
    public ResponseDTO cancelOrder(@RequestBody Map map){
        Integer r = orderService.cancelOrder(map);
        if(r == -1){
            return  new ResponseDTO().error("失败");
        }
        return  new ResponseDTO().success("取消成功");
    }

    

}
